package com.freight.user.controller;

import com.freight.core.common.utils.PageUtil;
import com.freight.core.common.utils.ResultUtil;
import com.freight.core.common.utils.SecurityUtil;
import com.freight.core.common.vo.PageVo;
import com.freight.core.common.vo.Result;
import com.freight.core.common.vo.SearchVo;
import com.freight.user.dto.UserLoginDto;
import com.freight.user.entity.UserLogin;
import com.freight.user.service.UserLoginService;
import com.freight.user.vo.UserAdminVo;
import com.freight.user.vo.UserInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author huyingtai
 */
@Slf4j
@RestController
@Api(description = "用户登入记录接口")
@RequestMapping("/freight/userLogin")
@Transactional
public class UserLoginController {

    @Autowired
    private UserLoginService userLoginService;

    @Autowired
    SecurityUtil securityUtil;

    @RequestMapping(value = "/cancellationUser", method = RequestMethod.POST)
    @ApiOperation(value = "注销用户")
    public Result<String> cancellationUser(Long userId) {
        userLoginService.cancellationUser(userId);
        return new ResultUtil().setData("操作成功");
    }

    @RequestMapping(value = "/getByCondition", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<Page<UserLogin>> getByCondition(UserLogin userLogin, SearchVo searchVo, PageVo pageVo) {
        Page<UserLogin> page = userLoginService.findByCondition(userLogin, searchVo, PageUtil.initPage(pageVo));
        return new ResultUtil<Page<UserLogin>>().setData(page);
    }

    @RequestMapping(value = "/updateUserLogin", method = RequestMethod.POST)
    @ApiOperation(value = "更新用户的登入时间和位置地理信息")
    public Result<String> updateUserLogin(UserLoginDto userLoginDto) {
        userLoginService.updateUserLogin(userLoginDto);
        return new ResultUtil().setData("保存成功");
    }

    @RequestMapping(value = "/getAdminInfo", method = RequestMethod.GET)
    @ApiOperation(value = "获取管理员信息")
    public Result<UserAdminVo> getAdminInfo() {
        UserAdminVo u = userLoginService.getAdminInfo();
        return new ResultUtil().setData(u);
    }

    @RequestMapping(value = "/updateRegistStatus", method = RequestMethod.GET)
    @ApiOperation(value = "审核接口")
    public Result<String> updateRegistStatus(Long userId,Integer registState,String reason) {
        String res = userLoginService.updateRegistStatus(userId,registState,reason);
        return new ResultUtil().setData(res);
    }

    @RequestMapping(value = "/userInfoVo", method = RequestMethod.GET)
    @ApiOperation(value = "获取当前登录用户接口")
    public Result<UserInfoVo> userInfoVo() {
        UserInfoVo userInfoVo = userLoginService.getUserInfoVo();
        return new ResultUtil<UserInfoVo>().setData(userInfoVo);
    }
}
